var page = 1;
Page({

  data: {
    noInfo: false,
    loading: false,
  },

  receive(e) {
    wx.post("receiveOrder", {
      id_kd_staff: wx.appData.staff.id_kd_staff,
      staff_phone: wx.appData.staff.staff_phone,
      id_kd_order: e.currentTarget.id,
    }, res => {
      if (res.ok) {
        wx.toast("接单成功", "success")
      } else {
        wx.toast("手慢了，订单被抢走啦")
      }
      this.onLoad();
    }, "请求接单")
  },

  onLoad() {
    page = 1;
    this.setData({ loading: true, noInfo: false });
    wx.post('getOrderList', {
      unionid: wx.appData.user.unionid
    }, res => {
      this.setData({ loading: false });
      wx.stopPullDownRefresh();
      if (!res.ok || !res.data.length) {
        this.setData({ orders: [], noInfo: true });
        return 0;
      }
      res.data.forEach(e => {
        e.id = (Array(6).join(0) + e.id_kd_order).slice(-6);
        e.price = parseFloat(e.price).toFixed(2);
      })
      this.setData({ orders: res.data });
      ++page;
    })
  },

  onPullDownRefresh() {
    this.onLoad();
  },
  
  onReachBottom: function () {
    if (this.data.noInfo) return;
    this.setData({ loading: true });
    wx.post('getOrderList', {
      unionid: wx.appData.user.unionid,
      page: page,
    }, res => {
      this.setData({ loading: false });
      if (!res.ok) {
        this.setData({ noInfo: true });
        return 0;
      }
      res.data.forEach(e => {
        e.id_kd_order = (Array(6).join(0) + e.id_kd_order).slice(-6);
        e.price = parseFloat(e.price).toFixed(2);
      })
      this.setData({ orders: this.data.orders.concat(res.data)})
      ++page;
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})